package com.tianruan.dc.modules.sys.user.service.impl;

import com.tianruan.dc.common.encoding.Md5PasswordEncoder;
import com.tianruan.dc.common.utils.IdGen;
import com.tianruan.dc.modules.sys.user.api.RegisterController;
import com.tianruan.dc.modules.sys.user.entity.dto.RegisterDTO;
import com.tianruan.dc.modules.sys.user.mapper.RegisterMapper;
import com.tianruan.dc.modules.sys.user.service.IRegisterService;
import com.tianruan.dc.modules.userinfo.entity.User;
import com.tianruan.dc.modules.userinfo.mapper.UserInfoMapper;
import com.tianruan.dc.modules.userinfo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 后台注册
 */
@Service
public class RegisterServiceImpl implements IRegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private RegisterController registerController;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 判断用户名是否为重复
     * @param username
     * @return
     */
    @Override
    public boolean isUsernameExist(String username) {
        return registerMapper.getIdByUsername(username) == null;
    }

    /**
     * 存入数据
     * @param registerDTO
     */
    @Override
    public void save(RegisterDTO registerDTO) {
        //以UUID为主键
        registerDTO.setId(IdGen.uuid());

        //MD5加密密码
        Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
        registerDTO.setPassword(md5PasswordEncoder.encode(registerDTO.getPassword()));
        //设置默认状态为已启用
        registerDTO.setStatus("1");

        //根据服务器时间得出当前更新时间与创造时间
        registerDTO.setCreateDate(LocalDateTime.now());
        registerDTO.setUpdateDate(LocalDateTime.now());
        //设置默认状态为正常
        registerDTO.setDelFlag("0");
        registerMapper.save(registerDTO);

        User user = new User();
        user.setId(IdGen.uuid());
        user.setUserId(registerDTO.getId());
        //若是导师
        if(registerDTO.getRoleId().equals("2")){
            user.setType("0");
        }
        //若是学生
        if (registerDTO.getRoleId().equals("3")){
            user.setType("1");
        }
        user.setPhone(registerDTO.getPhone());
        user.setName(registerDTO.getName());
        user.setCreateDate(registerDTO.getCreateDate());
        user.setDelFlag("0");
        registerMapper.insert(user);
    }


    /**
     * 添加角色
     *
     * @param id
     */
    @Override
    public void role(String id,String roleId) {
        registerMapper.role(id,roleId);
    }

    /**
     * 微信小程序添加角色
     * @param id
     * @param roleId
     */
    @Override
    public void WXrole(String id,String roleId) {
        registerMapper.WXrole(id,roleId);
    }

    /**
     * 微信小程序新增用户
     * @param user
     */
    @Override
    public void WXsave(User user) {
        //以UUID为主键
        user.setUserId(IdGen.uuid());

        //设置默认状态为已启用
        user.setStatus("1");

        //根据服务器时间得出当前更新时间与创造时间
        user.setCreateDate(LocalDateTime.now());

        //设置默认状态为正常
        user.setDelFlag("0");

//        userInfoMapper.WXsave(user);
    }

    @Override
    public boolean isNameExist(String name) {
        return registerMapper.getIdByName(name) == null;
    }


    /**
     * 角色授权
     * @param permissionId
     * @param idByUsername
     */
//    @Override
//    public void permission(Integer permissionId, RegisterDTO idByUsername) {
//        registerMapper.permission(permissionId, idByUsername.getUsername());
//    }


}
